package com.boshao.classschedule.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.boshao.classschedule.R;
import com.boshao.classschedule.model.DateInfo;
import com.boshao.classschedule.utils.ViewHolder;

/**
 * 日历Adapter
 * @author boshao
 *
 */
public class CalendarGridAdapter extends BaseAdapter{
	private Context mContext;
	private List<DateInfo> mDateInfos;
	private int mCurrentPosition = -1;
	private int mSelectedPosition = -1;
	public CalendarGridAdapter(Context context,List<DateInfo> dateInfos) {
		mContext = context;
		mDateInfos = dateInfos;
	}

	@Override
	public int getCount() {
		return mDateInfos.size();
	}

	@Override
	public Object getItem(int position) {
		return mDateInfos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.gridview_calendar_item,parent,false);
		}
		TextView tvDay = ViewHolder.get(convertView, R.id.item_day);
		TextView tvLunarDay = ViewHolder.get(convertView, R.id.item_lunar_day);
		DateInfo dateInfo = mDateInfos.get(position);
		tvDay.setText(dateInfo.getDate() + "");
		tvLunarDay.setText(dateInfo.getNongliDate());
		if (mCurrentPosition == position) {
			tvDay.setTextColor(Color.BLUE);
			tvLunarDay.setTextColor(Color.BLUE);
			convertView.setBackgroundColor(mContext.getResources().getColor(R.color.week_color));
		} else if(mSelectedPosition == position){
			convertView.setBackgroundColor(mContext.getResources().getColor(R.color.gray));
		}else {
			convertView.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
			tvDay.setTextColor(Color.BLACK);
			tvLunarDay.setTextColor(Color.BLACK);
			if (dateInfo.isHoliday())
				tvLunarDay.setTextColor(Color.WHITE);
			else if (dateInfo.isThisMonth() == false) {
				tvDay.setTextColor(mContext.getResources().getColor(R.color.not_this_month_color));
			}
			else if (dateInfo.isWeekend()) {
				tvDay.setTextColor(mContext.getResources().getColor(R.color.week_color));
			}
		}
		if (dateInfo.getNongliDate().length() > 3){
			tvLunarDay.setTextSize(10);
		}	
		if (dateInfo.getNongliDate().length() >= 5){
			tvLunarDay.setTextSize(8);
		}	
		return convertView;
	}
	
	/**
	 * 设置选中位置
	 * */
	public void setSelectedPosition(int position) {  
        mSelectedPosition = position;  
    }  
	
	/**
	 * 设置日历今天的位置
	 * @param position
	 */
	public void setCurrentPosition(int position) {  
		mCurrentPosition = position;  
    }

}
